package com.ctg.behavior.calc.impala.calculator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.api.CtCalcStatus;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.CtCalculator;
import com.ctg.behavior.calc.impala.builder.tracks.CtTracksCalcResultBuilder;
import com.ctg.behavior.calc.impala.engine.ImpalaSQLEngine;
import com.ctg.behavior.calc.impala.transfer.CtTotalTransfer;
import com.ctg.behavior.calc.impala.transfer.tracks.CtTracksUnionTransfer;
import com.ctg.behavior.calc.impala.utils.CtCalculatorUtils;
import com.ctg.behavior.client.impala.common.CtQueryResult;
import com.ctg.behavior.common.calc.CtCalcBody;
import com.ctg.behavior.common.calc.CtCalcResult;
import com.ctg.behavior.common.calc.CtCalcTotal;
import com.ctg.behavior.common.consts.CtCalcTaskConsts;
import com.ctg.behavior.common.consts.CtFieldConsts;
import com.ctg.behavior.common.tracks.CtTracksBody;
import com.ctg.behavior.common.tracks.CtTracksResult;
import com.ctg.behavior.utils.CtDateUtils;
import com.ctg.behavior.utils.CtStatusUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtTracksCalculator implements CtCalculator {

	@Autowired
	private ImpalaSQLEngine sqlEngine;
	@Autowired
	private CtTracksUnionTransfer unionTracksTransfer;
	@Autowired
	private CtTotalTransfer ctTotalTransfer;
	@Autowired
	private CtTracksCalcResultBuilder resultBuilder;

	@Override
	public CtCalcResult doCalculator(CtCalcBody calcBody) {
		CtTracksResult calcResult = new CtTracksResult();
		if (!(calcBody instanceof CtTracksBody)) {
			log.error("calcBody={}", calcBody.getClass());
			CtStatusUtils.fillStatus(calcResult, CtCalcStatus.BodyNoMatch);
			return calcResult;
		}

		// 1.
		log.info("doTransfer-start: {}", CtDateUtils.getCurrentDateSSS());
		CtTracksBody tracksCalcBody = (CtTracksBody) calcBody;
		String detailSQL = unionTracksTransfer.doTransfer(tracksCalcBody, CtSQLParam.createParam());
		log.info("doTransfer-end: {}", CtDateUtils.getCurrentDateSSS());

		// 2.计算总数
		{
			log.info("totalCalc-start: {}", CtDateUtils.getCurrentDateSSS());
			CtCalcTotal calcTotal = new CtCalcTotal();
			calcTotal.setDetailSQL(detailSQL);
			String totalSQL = ctTotalTransfer.doTransfer(calcTotal, CtSQLParam.createParam());
			String queryType = CtCalcTaskConsts.TASK_TYPE_USERSLIST + "@TRACKS@TOTAL";
			CtQueryResult totalResult = sqlEngine.doQuery(queryType, totalSQL);
			log.info("totalResult={}", JSON.toJSONString(totalResult));
			Integer countTotal = 0;
			if(!totalResult.getRows().isEmpty()) {
				countTotal = Integer.valueOf(totalResult.getRows().get(0).get(CtFieldConsts.FIELD_KEY_TOTAL).toString());
			}
			if (tracksCalcBody.getLimit() <= 0 || tracksCalcBody.getLimit() > countTotal) {
				calcResult.getValue().setTotal(countTotal);
			} else {
				calcResult.getValue().setTotal(tracksCalcBody.getLimit());
			}
			log.info("totalCalc-end: {}", CtDateUtils.getCurrentDateSSS());
		}
		// 3.计算明细
		{
			log.info("time - start: {}", CtDateUtils.getCurrentDateSSS());
			String queryType = CtCalcTaskConsts.TASK_TYPE_TRACKS;
			CtQueryResult queryResult = sqlEngine.doQuery(queryType, detailSQL);
			log.info("queryResult={}", JSON.toJSONString(queryResult.getRows()));
			CtCalculatorUtils.fillCalcResult(calcResult, queryResult);
			if (!calcResult.getSuccess()) {
				log.error(queryResult.getCode());
			} else {
				// 处理计算结果ROWS
				calcResult = resultBuilder.doBuilder(calcResult, queryResult.getRows(), tracksCalcBody);
			}
			log.info("time - end: {}", CtDateUtils.getCurrentDateSSS());
		}

		return calcResult;
	}

}
